package com.woniuxy.amusementparkos.service.xxy.impl;

import java.util.List;
import java.util.UUID;

import javax.management.RuntimeErrorException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.amusementparkos.dao.xxy.StoreDao;
import com.woniuxy.amusementparkos.entity.xxy.Store_Info;
import com.woniuxy.amusementparkos.exception.xxy.GetKeyException;
import com.woniuxy.amusementparkos.exception.xxy.InsertException;
import com.woniuxy.amusementparkos.exception.xxy.SelectException;
import com.woniuxy.amusementparkos.service.xxy.StoreService;
@Service
public class StoreServiceImpl implements StoreService {
	
	@Autowired
	StoreDao storeDao;

	@Override
	public void insertStore(Store_Info store) {
		try {
			storeDao.insertStore(store);
		} catch (Exception e) {
			e.printStackTrace();
			throw new InsertException("添加失败,请重试");
		}
		
		
	}
	/**
	 * 管理员修改商户信息
	 */
	@Override
	public void updateStore_manager(Store_Info store) {
		try {
			storeDao.updateStore_manager(store);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SelectException("系统出错,请稍后再试或联系管理员");
		}
		
		
	}
	/**
	 * 商家修改自己商户信息
	 */
	@Override
	public void updateStore_store(Store_Info store) {
		try {
			storeDao.updateStore_store(store);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SelectException("系统出错,请稍后再试或联系管理员");
		}
		
	}
	/**
	 * 查询单个商店
	 */
	@Override
	public Store_Info selectStoreByStore_id(int store_id) {
		try {
			Store_Info store = storeDao.selectStoreByStore_id(store_id);
			return store;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SelectException("系统出错,请稍后再试或联系管理员");
		}
	}
	/**
	 * 查询多个商店
	 */
	@Override
	public List<Store_Info> selectAllStore(int page,int pageSize) {
		try {
			return storeDao.selectAllStore(page, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SelectException("系统出错,请稍后再试或联系管理员");
		}
	}
	/**
	 * 查询所有商家的条数
	 */
	@Override
	public long selectAllStoreCount() {
		try {
			long count = storeDao.selectAllStoreCount();
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SelectException("系统出错,请稍后再试或联系管理员");
		}
		
	}
	@Override
	public void deleteStore(int store_id) {
		try {
			storeDao.deleteStore(store_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SelectException("删除出错,请稍后再试或联系管理员");
		}
		
	}
	@Override
	public String getKey(int store_id) {
		Store_Info store = new Store_Info();
		try {
			 store = storeDao.selectStoreByStore_id(store_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SelectException("系统出错,请稍后再试或联系管理员");
		}
		if(store.getStore_key()!=null) throw new GetKeyException("此商家已经生成过了key,它的key为:"+store.getStore_key());
		store.setStore_key(UUID.randomUUID().toString().replace("-", ""));
		try {
			storeDao.setStore_key(store);
			return store.getStore_key();
		} catch (Exception e) {
			e.printStackTrace();
			throw new SelectException("系统出错,请稍后再试或联系管理员");
		}
		
	}

}
